#if __ANDROID__
#include "sys.h"
#include "log.h"
#include "devinfo.h"

#define PROBE_CONF_FILE "../../probe.cfg"

static void _get_manufacturer(char *buf){
    char buffer[64], *ptr;
    char *delim = " ", *saveptr;
    __system_property_get("ro.product.manufacturer", buffer);
    ptr = strtok_r(buffer, delim, &saveptr);
    if(strcmp(ptr,"unknown") == 0){
        __system_property_get("ro.build.user", buffer);//tcl
        ptr = buffer;
    }
    strcpy(buf, ptr);
}

static void _get_cfg(char *name, char *value){
    char line[128] = {0};
    char *delim = "=", *saveptr = NULL, *ptr = NULL;
    FILE *fp = NULL;

    fp = fopen(PROBE_CONF_FILE, "r");
    if(!fp){
        return;
    }

    while(fgets(line, sizeof(line), fp) != NULL){
        line[strlen(line) - 3] = '\0';
        if(strstr(line, name)){
            ptr = strtok_r(line, delim, &saveptr);
            ptr = strtok_r(NULL, delim, &saveptr);
            strcpy(value, ptr);
            break;
        }
    }
    fclose(fp);
}
static void _get_info_from_cfg(DevInfo_t *devinfo){
    char acc_mode[32] = {0};

    _get_cfg("VENDOR", devinfo->factory);
    _get_cfg("DEVICEMODULE", devinfo->model);
    _get_cfg("SWVERSION", devinfo->sw_version);
    _get_cfg("OSVERSION", devinfo->osVersion);
    _get_cfg("MACADDRESS", devinfo->mac);
    _get_cfg("STBIP", devinfo->ip);
    _get_cfg("ACCESSMODE", acc_mode);
    if(strcmp(acc_mode, "Cable") == 0){
        devinfo->is_wifi = 0;
    }else{
        devinfo->is_wifi = 1;
    }
}

void android_get_devinfo(DevInfo_t *devinfo){
    strcpy(devinfo->osType, "Android");
    // strcpy(devinfo->dev_type, "TV");
    strcpy(devinfo->dev_type, "ONT");

    if(access(PROBE_CONF_FILE, F_OK) == 0){
        _get_info_from_cfg(devinfo);
        return;
    }
    _get_manufacturer(devinfo->factory);
    __system_property_get("ro.product.model", devinfo->model);
    __system_property_get("ro.build.version.incremental", devinfo->sw_version);
    __system_property_get("ro.build.version.release", devinfo->osVersion);
}
#endif //__ANDROID__